package cn.dzhang.rcp.hyperbola.dialogs;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.PojoProperties;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import cn.dzhang.rcp.hyperbola.model.ContactEntry;

public class AddContactDialog extends TitleAreaDialog {
	private Text txtName;
	private Text txtNickname;
	private Text txtServer;
	private ContactEntry entry;

	/**
	 * Create the dialog.
	 *
	 * @param parentShell
	 */
	public AddContactDialog(Shell parentShell) {
		super(parentShell);
		Object data = parentShell.getData();
		if (data instanceof ContactEntry) {
			entry = (ContactEntry) data;
		} else {
			entry = new ContactEntry();
		}
	}

	public ContactEntry getEntry() {
		return entry;
	}

	/**
	 * Create contents of the dialog.
	 *
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		setMessage("Search");
		setTitle("Add Contact");
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		container.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				true, 1, 1));
		GridLayout gl_container = new GridLayout(2, false);
		gl_container.verticalSpacing = 15;
		container.setLayout(gl_container);

		Label lblName = new Label(container, SWT.NONE);
		lblName.setText("Name: ");

		txtName = new Text(container, SWT.BORDER);
		GridData gd_txtName = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_txtName.widthHint = 250;
		txtName.setLayoutData(gd_txtName);

		Label lblNickname = new Label(container, SWT.NONE);
		lblNickname.setText("Nickname: ");

		txtNickname = new Text(container, SWT.BORDER);
		txtNickname.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));

		Label lblServer = new Label(container, SWT.NONE);
		lblServer.setText("Address: ");

		txtServer = new Text(container, SWT.BORDER);
		txtServer.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));

		return area;
	}

	/**
	 * Create contents of the button bar.
	 *
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
		initDataBindings();
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(440, 351);
	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		IObservableValue observeTextTxtNameObserveWidget = WidgetProperties
				.text(SWT.FocusOut).observe(txtName);
		IObservableValue nameEntryObserveValue = PojoProperties.value("name")
				.observe(entry);
		bindingContext.bindValue(observeTextTxtNameObserveWidget,
				nameEntryObserveValue, null, null);
		//
		IObservableValue observeTextTxtNicknameObserveWidget = WidgetProperties
				.text(SWT.FocusOut).observe(txtNickname);
		IObservableValue nicknameEntryObserveValue = PojoProperties.value(
				"nickname").observe(entry);
		bindingContext.bindValue(observeTextTxtNicknameObserveWidget,
				nicknameEntryObserveValue, null, null);
		//
		IObservableValue observeTextTxtServerObserveWidget = WidgetProperties
				.text(SWT.FocusOut).observe(txtServer);
		IObservableValue serverEntryObserveValue = PojoProperties.value(
				"server").observe(entry);
		bindingContext.bindValue(observeTextTxtServerObserveWidget,
				serverEntryObserveValue, null, null);
		//
		return bindingContext;
	}
}
